
import LanguageMng from "../../Managers/LanguageMng";

cc.Class({
    extends: cc.Component,

    properties: {
        cardParent:{
            default:null,
            type:cc.Node
        },

        labelName:{
            default:null,
            type:cc.Label
        },

        labelWin:{
            default:null,
            type:cc.Label
        },

        iconWin:{
            default:null,
            type:cc.Node
        },

        labelScore:{
            default:null,
            type:cc.Label
        },

        labelTotalScore:{
            default:null,
            type:cc.Label
        },

        bgParent:{
            default:null,
            type:cc.Node
        },

        backPrefab:{
            default:null,
            type:cc.Prefab
        },

        bottomModel:{
            default:null,
            type:cc.Prefab
        },

        botFrameGreen:{
            default:null,
            type:cc.SpriteFrame
        },

        botFrameRed:{
            default:null,
            type:cc.SpriteFrame
        },

        botFrameYellow:{
            default:null,
            type:cc.SpriteFrame
        },

        botFrameBlue:{
            default:null,
            type:cc.SpriteFrame
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.iconWin.active = false
    },

    start () {

    },

    // update (dt) {},

    changeBgColor:function (bgFrame) {
        for(let i=0;i<this.bgParent.children.length;i++){
            let bgBlock = this.bgParent.children[i]
            bgBlock.getComponent(cc.Sprite).spriteFrame = bgFrame
        }
    },

    refreshCards:function (cardArrays,matchs,mixNum) {
        this._initRowCards(this.cardParent,cardArrays,matchs,mixNum)
    },

    initEmptyCards:function () {
        this._initRowEmptyCards(this.cardParent)
    },

    setName:function (name) {
        this.labelName.string = name
    },

    setWin:function (win) {
        if(win){
            this.iconWin.active = true
            this.labelWin.node.active = false
        }else {
            this.iconWin.active = false
            this.labelWin.node.active = true
            this.labelWin.string = LanguageMng.getSentence(229)
        }
    },

    setWaitting:function (string) {
        if(string === "%win%"){
            this.setWin(true)
        }else if(string === "%lose%"){
            this.setWin(false)
        }else {
            this.labelWin.string = string
        }
    },

    //game score,to calculate game situation,no need to converse to rupee at any time
    setScore:function (score) {
        this.labelScore.string = score
    },

    //game final won or lose currency
    setTotalScore:function (totalScoreStr) {
        this.labelTotalScore.string = totalScoreStr
    },

    _initRowCards:function (cardParent,cards,matchs,mixNum) {
        let botInterval = 10
        cardParent.removeAllChildren(true)

        let nowX = 0
        for(let i=0;i<cards.length;i++){
            let pokerArr = cards[i]
            let newBot = cc.instantiate(this.bottomModel)
            cardParent.addChild(newBot)
            newBot.getComponent("ResultBottom").setBottomColor(pokerArr,mixNum)
            let typeObj = newBot.getComponent("ResultBottom").typeObj
            let giveCards = []
            if(matchs && !typeObj){
                for(let j=0;j<matchs.length;j++){
                    let pokerValue = matchs[j]
                    if(pokerArr.indexOf(pokerValue) > -1){
                        matchs.splice(j--,1)
                        giveCards.push(pokerValue)
                    }
                }
            }
            let botLength = newBot.getComponent("ResultBottom").initResultBottom(pokerArr,giveCards,mixNum)

            newBot.x = nowX + botLength/2
            nowX = newBot.x + botLength/2 + botInterval
        }

        let hasGreen = false
        let seqCount = 0
        for(let i=0;i<cardParent.children.length;i++){
            let cardNode = cardParent.children[i]
            let typeObj = cardNode.getComponent("ResultBottom").typeObj
            if(!typeObj){
                continue
            }

            if(typeObj.isSequence){
                if(typeObj.isPure) hasGreen = true
                seqCount++
            }
        }
        cc.logcal("存在Pure Sequence，sequence数量为：",seqCount)

        if(hasGreen){
            for(let i=0;i<cardParent.children.length;i++){
                let cardNode = cardParent.children[i]
                let script = cardNode.getComponent("ResultBottom")
                let typeObj = script.typeObj
                if(!typeObj){
                    cc.logcal("没有任何类型：",script.cards)
                    script.setBottomFrame(this.botFrameRed)
                    continue
                }

                if(typeObj.isSequence){
                    cc.logcal("是顺子：",script.cards)
                    script.setBottomFrame(this.botFrameGreen)
                }else if(typeObj.isSet){
                    if(seqCount > 1){
                        cc.logcal("是Set：",script.cards)
                        script.setBottomFrame(this.botFrameBlue)
                    }else {
                        cc.logcal("是Set：",script.cards)
                        script.setBottomFrame(this.botFrameYellow)
                    }
                }else {
                    cc.warn("有type却什么都不是?：",typeObj,"  ",script.cards)
                }
            }
        }else {
            for(let i=0;i<cardParent.children.length;i++){
                let cardNode = cardParent.children[i]
                let script = cardNode.getComponent("ResultBottom")
                let typeObj = script.typeObj
                if(!typeObj){
                    cc.logcal("没有任何类型：",script.cards)
                    script.setBottomFrame(this.botFrameRed)
                    continue
                }

                cc.logcal("有类型，但没有PureSequence，黄色：",script.cards)
                script.setBottomFrame(this.botFrameYellow)
            }
        }
    },

    _initRowEmptyCards:function (cardParent) {
        let botInterval = 2
        // for(let i=0;i<cardParent.children.length;i++){
        //     cardParent.children[i].destroy()
        // }
        cardParent.removeAllChildren(true)

        for(let i=0;i<13;i++){
            let newPoker = cc.instantiate(this.backPrefab)
            cardParent.addChild(newPoker)
            newPoker.x = (newPoker.width/2 + botInterval) * (i+1)
        }
    },
});
